
import gov.nih.mipav.model.structures.ModelImage;
import gov.nih.mipav.view.ViewUserInterface;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Enumeration;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JSDice extends Method implements Drawable, ActionListener,
		MethodRunnable {

	private JButton diceButton;

	private JButton runButton;

	private DefaultComboBoxModel firstImageModel = null;

	private DefaultComboBoxModel secondImageModel = null;

	private JFormattedTextField firstLabel = null;

	private JFormattedTextField secondLabel = null;

	public JSDice() {
		RUNNING_STATE = "Obtaining coefficient";
		DONE_STATE = "Dice coefficient obtained";
	}

	public Component drawContentPane() {
		diceButton = new JButton("Dice");
		diceButton.setActionCommand("DICE");
		diceButton.addActionListener(this);

		return diceButton;
	}

	public void setEnable(boolean enable) {
		if (diceButton != null)
			diceButton.setEnabled(enable);
	}

	// CHECK WHO SENT THE EVENT
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();

		if (command.equals("DICE")) {
			viewDiceParameters();
		} else if (command.equals("RUNDICE")) {
			run();
		}
	}

	protected void runAlgorithm() {
		// Thread segmentationThread = new Thread(this);
		// segmentationThread.start();
	}

	public void run() {
		StatusManager.getInstance().setState(RUNNING_STATE);
		MethodManager jsMethodManager = MethodManager.getInstance();
		jsMethodManager.setEnable(false);

		ViewUserInterface userInterface = ViewUserInterface.getReference();

		ModelImage firstImage = userInterface
				.getRegisteredImageByName((String) firstImageModel
						.getSelectedItem());
		ModelImage secondImage = userInterface
				.getRegisteredImageByName((String) secondImageModel
						.getSelectedItem());

		int size = firstImage.getSize();

		double[] firstImageData = new double[size];
		double[] secondImageData = new double[size];

		double XY = 0.0;
		double X = 0.0;
		double Y = 0.0;

		try {
			firstImage.exportData(0, size, firstImageData);
			secondImage.exportData(0, size, secondImageData);

			for (int i = 0; i < size; i++) {
				if (((Double) firstLabel.getValue()) == firstImageData[i])
					X++;
				if (((Double) secondLabel.getValue()) == secondImageData[i])
					Y++;
				if (((Double) firstLabel.getValue()) == firstImageData[i]
						&& ((Double) secondLabel.getValue()) == secondImageData[i])
					XY++;
			}

			double value = 2 * XY / (X + Y);
			JOptionPane.showMessageDialog(userInterface.getMainFrame(), "DICE "
					+ value);

		} catch (IOException e) {
			e.printStackTrace();
		}

		jsMethodManager.setEnable(true);
		StatusManager.getInstance().setState(DONE_STATE);
	}

	private void viewDiceParameters() {
		firstImageModel = new DefaultComboBoxModel();
		secondImageModel = new DefaultComboBoxModel();
		firstLabel = new JFormattedTextField(new Double(0));
		secondLabel = new JFormattedTextField(new Double(0));

		ViewUserInterface userInterface = ViewUserInterface.getReference();

		int numberOfImages = userInterface.getRegisteredImagesNum();
		if (numberOfImages > 0) {
			Enumeration<String> names = userInterface.getRegisteredImageNames();
			while (names.hasMoreElements()) {
				String temp = names.nextElement();
				firstImageModel.addElement(temp);
				secondImageModel.addElement(temp);
			}
		}

		JPanel firstPane = new JPanel();

		firstPane.setLayout(new FlowLayout());
		JComboBox firstImageComboBox = new JComboBox(firstImageModel);
		firstImageComboBox.setMinimumSize(new Dimension(100, 20));
		firstPane.add(firstImageComboBox);
		firstPane.add(firstLabel);

		JPanel secondPane = new JPanel();

		secondPane.setLayout(new FlowLayout());
		JComboBox secondImageComboBox = new JComboBox(secondImageModel);
		firstImageComboBox.setMinimumSize(new Dimension(100, 20));
		secondPane.add(secondImageComboBox);
		secondPane.add(secondLabel);

		runButton = new JButton("Run");
		runButton.setActionCommand("RUNDICE");
		runButton.addActionListener(this);

		JDialog dialog = new JDialog();
		dialog.getContentPane().setLayout(new BorderLayout());
		dialog.getContentPane().add(firstPane, BorderLayout.NORTH);
		dialog.getContentPane().add(secondPane, BorderLayout.CENTER);
		dialog.getContentPane().add(runButton, BorderLayout.SOUTH);
		dialog.pack();
		dialog.setVisible(true);
	}
}
